/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PolyResultFrame.java
 *
 * Created on Mar 19, 2012, 10:59:10 PM
 */
package gui;

import db.DatabaseManager;
import db.entity.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author suzann
 */
public class PolyResultFrame extends javax.swing.JFrame {

    /** Creates new form PolyResultFrame */
    public PolyResultFrame() {
        _instance = this;
        initComponents();
        getPadLotID();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbResult = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jcbPadBatchID = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jcbStation = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        jtbResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Hours", "Vacuum (mmHg)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtbResult);
        jtbResult.getColumnModel().getColumn(0).setMinWidth(50);
        jtbResult.getColumnModel().getColumn(0).setPreferredWidth(50);
        jtbResult.getColumnModel().getColumn(0).setMaxWidth(52);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setPreferredSize(new java.awt.Dimension(552, 65));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Pad Batch ID");

        jcbPadBatchID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPadBatchIDActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Station Arm");

        jcbStation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "L01", "R01", "L02", "R02", "L03", "R03", "L04", "R04", "L05", "R05", "L06", "R06", "L07", "R07" }));
        jcbStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbStationActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jcbStation, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jcbPadBatchID, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jcbStation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbPadBatchID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>

    private void jcbStationActionPerformed(java.awt.event.ActionEvent evt) {                                           
        setDefaultValue();
    }                                          

    private void jcbPadBatchIDActionPerformed(java.awt.event.ActionEvent evt) {                                              
        if(jcbPadBatchID.getItemCount() > 0)
            setDefaultValue();
    }                                             

    
    private void setDefaultValue()
    {
        String stationArmId, stationId, armId, padLotId;
        
        if(jcbStation.getSelectedItem() == null)
            return ;
    	if(jcbPadBatchID.getSelectedItem() == null)
    		return ;
        
        stationArmId = jcbStation.getSelectedItem().toString();
    	stationId = stationArmId.substring(1, 3);
    	armId = stationArmId.substring(0, 1);
    	padLotId = jcbPadBatchID.getSelectedItem().toString();
        
        DatabaseManager db = new DatabaseManager();
        List<PolyResult> pList = db.listPolyResultByStationPadID(stationId, armId, padLotId );
        int row = pList.size();

        Object[][] tableData = new Object[row][2];
        
        for(int i=0; i<row; i++)
        {
            PolyResult prItem = pList.get(i);
            tableData[i][0] = i + 1;
            tableData[i][1] = prItem.getTapervalue();
        }
        
        String [] columeNames = new String [] {
                "Hours", "Vacuum (mmHg)"
            };
        
        DefaultTableModel tableModel = new DefaultTableModel(tableData, columeNames);
        jtbResult.setModel(tableModel);
        
        jtbResult.getColumnModel().getColumn(0).setMinWidth(50);
        jtbResult.getColumnModel().getColumn(0).setPreferredWidth(50);
        jtbResult.getColumnModel().getColumn(0).setMaxWidth(50);
    	
        jtbResult.invalidate();
        jtbResult.validate();
    }
    
    private void getPadLotID()
    {
    	jcbPadBatchID.removeAllItems();
    	DatabaseManager db = new DatabaseManager();
    	List<Padbatch> pList = db.getPadBatchID();
    	for(Padbatch pItem : pList)
    		jcbPadBatchID.addItem(pItem.getPadBatchID());

    }
    
    public static void updatePadBatchID()
    {
    	_instance.getPadLotID();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PolyResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PolyResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PolyResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PolyResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PolyResultFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbPadBatchID;
    private javax.swing.JComboBox jcbStation;
    private javax.swing.JTable jtbResult;
    // End of variables declaration
    private static PolyResultFrame _instance;
    
}
